package com.motu.monstercity.server.game.logic;

import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.userdata.*;
import com.motu.vertx.module.utility.model.EntityManager;
import com.motu.vertx.module.utility.model.Model;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import io.vertx.core.json.JsonArray;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class UnionUserData extends UnionBaseData {
    public static UnionInfo getUnionInfo(long unionId) {
        return (UnionInfo) EntityManager.getEntity(UnionInfo.class, unionId);
    }
    public static UserUnion getUserUnion(long userId) {
        return (UserUnion) EntityManager.getUniqueEntity(UserUnion.class, userId);
    }

    public static List<UserUnion> getUserUnionList(long unionId) {
        List<UserUnion> result = new ArrayList<>();
        List<Model> list = EntityManager.getEntityList(UserUnion.class, unionId);
        if (list != null) {
            for (Model model : list) {
                UserUnion userUnion = (UserUnion) model;
                list.add(userUnion);
            }
        }
        return result;
    }
    public static int getUserUnionNum(long unionId) {
        return (int) EntityManager.getEntityCount(UserUnion.class, unionId);
    }

    public static UnionUserApply getUnionUserApply(long unionId, long userId) {
        return (UnionUserApply) EntityManager.getEntity(UnionUserApply.class, unionId, userId);
    }

    public static UnionMember getUnionMember(long unionId,long userId) {
        return (UnionMember) EntityManager.getEntity(UnionMember.class, unionId,userId);
    }

    public static int getUnionMemberNum(long unionId) {
        return (int) EntityManager.getEntityCount(UnionMember.class, unionId);
    }


    public static List<UnionMember> getUnionMemberList(long unionId) {
        List<UnionMember> result = new ArrayList<>();
        List<Model> list = EntityManager.getEntityList(UnionMember.class, unionId);
        if (list != null) {
            for (Model model : list) {
                UnionMember userUnion = (UnionMember) model;
                result.add(userUnion);
            }
        }
        return result;
    }

    public static JsonArray getUnionMemberIdList(long unionId) {
        return EntityManager.getEntityKeyList(UnionMember.class, unionId);
    }

    /**
     * 获取联盟的玩家申请列表
     */
    public static List<UnionUserApply> getUnionUserApplyList(long unionId) {
        List<Model> secrList = EntityManager.getEntityList(UnionUserApply.class, unionId);
        List<UnionUserApply> list = new ArrayList<>();
        if (secrList == null || secrList.isEmpty()) {
            return list;
        }
        for (Model model : secrList) {
            UnionUserApply entity = (UnionUserApply) model;
            list.add(entity);
        }
        Collections.sort(list, new Comparator<UnionUserApply>() {
            @Override
            public int compare(UnionUserApply o1, UnionUserApply o2) {
                return o1.getApplyTime() < o2.getApplyTime() ? -1
                        : o1.getApplyTime() > o2.getApplyTime() ? 1 : 0;
            }
        });
        return list;
    }

    public static UserUnionApply getUserUnionApply(long userId, long unionId) {
        return (UserUnionApply) EntityManager.getEntity(UserUnionApply.class, userId, unionId);
    }

    /**
     * 获取玩家的联盟申请列表
     */
    public static List<UserUnionApply> getUserUnionApplyList(long userId) {
        List<Model> secrList = EntityManager.getEntityList(UserUnionApply.class, userId);
        List<UserUnionApply> list = new ArrayList<>();
        if (secrList == null || secrList.isEmpty()) {
            return list;
        }
        for (Model model : secrList) {
            UserUnionApply entity = (UserUnionApply) model;
            list.add(entity);
        }
        Collections.sort(list, new Comparator<UserUnionApply>() {
            @Override
            public int compare(UserUnionApply o1, UserUnionApply o2) {
                return o1.getApplyTime() < o2.getApplyTime() ? -1
                        : o1.getApplyTime() > o2.getApplyTime() ? 1 : 0;
            }
        });
        return list;
    }

    /**
     * 获取一个职位的人数
     */
    public static int getPostNum(long unionId, int post) {
        int num = 0;
        List<UnionMember> unionMemberList = UnionManager.getUnionMemberList(unionId);
        for (UnionMember unionMember : unionMemberList) {
            UserUnion userUnion = getUserUnion(unionMember.getUserId());
            if (userUnion.getPost() == post) {
                num++;
            }
        }
        return num;
    }


    public static UnionBuild getUnionBuild(long unionId,int buildId) {
        return (UnionBuild) EntityManager.getEntity(UnionBuild.class, unionId,buildId);
    }

    public static List<UnionBuild> getUnionBuildList(long unionId) {
        List<UnionBuild> result = new ArrayList<>();
        List<Model> list = EntityManager.getEntityList(UnionBuild.class, unionId);
        if (list != null) {
            for (Model model : list) {
                UnionBuild unionBuild = (UnionBuild) model;
                result.add(unionBuild);
            }
        }
        return result;
    }

    public static UnionBuildUserRecord getUnionBuildUserRecord(long unionId,int buildId, long userId) {
        return (UnionBuildUserRecord) EntityManager.getEntity(UnionBuildUserRecord.class, unionId+"-"+buildId, userId);
    }

    public static List<UnionBuildUserRecord> getUnionBuildUserRecordList(long unionId,int buildId) {
        List<UnionBuildUserRecord> result = new ArrayList<>();
        List<Model> list = EntityManager.getEntityList(UnionBuildUserRecord.class, unionId+"-"+buildId);
        if (list != null) {
            for (Model model : list) {
                UnionBuildUserRecord unionBuildUserRecord = (UnionBuildUserRecord) model;
                result.add(unionBuildUserRecord);
            }
        }
        return result;
    }

    public static UnionBuildUserAssistRecord getUnionBuildUserAssistRecord(long unionId,int buildId, long userId) {
        return (UnionBuildUserAssistRecord) EntityManager.getEntity(UnionBuildUserAssistRecord.class, unionId+"-"+buildId, userId);
    }

    public static List<UnionBuildUserAssistRecord> getUnionBuildUserAssistRecordList(long unionId,int buildId) {
        List<UnionBuildUserAssistRecord> result = new ArrayList<>();
        List<Model> list = EntityManager.getEntityList(UnionBuildUserAssistRecord.class, unionId+"-"+buildId);
        if (list != null) {
            for (Model model : list) {
                UnionBuildUserAssistRecord unionBuildUserAssistRecord = (UnionBuildUserAssistRecord) model;
                result.add(unionBuildUserAssistRecord);
            }
        }
        return result;
    }


    public static UnionBossUserRecord getUnionBossUserRecord(long unionId,int bossId, long userId) {
        return (UnionBossUserRecord) EntityManager.getEntity(UnionBossUserRecord.class, unionId+"-"+bossId, userId);
    }

    public static List<UnionBossUserRecord> getUnionBossUserRecordList(long unionId,int bossId) {
        List<UnionBossUserRecord> result = new ArrayList<>();
        List<Model> list = EntityManager.getEntityList(UnionBossUserRecord.class, unionId+"-"+bossId);
        if (list != null) {
            for (Model model : list) {
                UnionBossUserRecord unionBossUserRecord = (UnionBossUserRecord) model;
                result.add(unionBossUserRecord);
            }
        }
        Collections.sort(result, new Comparator<UnionBossUserRecord>() {
            @Override
            public int compare(UnionBossUserRecord o1, UnionBossUserRecord o2) {
                return o1.getHp() > o2.getHp() ? -1 : 1;
            }
        });
        return result;
    }

    public static JsonArray getUnionBossUserRecordIdList(long unionId,int bossId) {
        return EntityManager.getEntityKeyList(UnionBossUserRecord.class, unionId+"-"+bossId);
    }


    public static UserUnionBossReward getUserUnionBossReward(long userId,int stageId) {
        return (UserUnionBossReward) EntityManager.getEntity(UserUnionBossReward.class, userId,stageId);
    }


    //获取联盟捐献模块信息
    public static UnionDonateInfo getUnionDonateInfo(long unionId) {
        return (UnionDonateInfo) EntityManager.getEntity(UnionDonateInfo.class, unionId);
    }

    //获取联盟运输中心信息
    public static UnionBusinessTaskInfo getUnionBusinessTaskInfo(long unionId) {
        return (UnionBusinessTaskInfo) EntityManager.getEntity(UnionBusinessTaskInfo.class, unionId);
    }


    //获取联盟折扣商店信息
    public static UnionDiscountShopInfo getUnionDiscountShopInfo(long unionId) {
        return (UnionDiscountShopInfo) EntityManager.getEntity(UnionDiscountShopInfo.class, unionId);
    }

    //获取联盟限时充值信息
    public static UnionActChargeInfo getUnionActChargeInfo(long unionId) {
        return (UnionActChargeInfo) EntityManager.getEntity(UnionActChargeInfo.class, unionId);
    }

    //获取联盟限时充值信息
    public static UnionBossInfo getUnionBossInfo(long unionId) {
        return (UnionBossInfo) EntityManager.getEntity(UnionBossInfo.class, unionId);
    }

    //获取联盟宝箱信息
    public static UnionBoxInfo getUnionBoxInfo(long unionId) {
        Model model = EntityManager.getEntity(UnionBoxInfo.class, unionId);
        return model == null ? null : (UnionBoxInfo) model;
    }

    // 获取礼盒列表
    public static  List<UnionGiftBoxList> getUnionGiftBoxList(long unionId) {
        List<UnionGiftBoxList> result = new ArrayList<>();
        List<Model> list = EntityManager.getEntityList(UnionGiftBoxList.class, unionId);
        if (list != null) {
            for (Model model : list) {
                UnionGiftBoxList unionGiftBoxList = (UnionGiftBoxList) model;
                result.add(unionGiftBoxList);
            }
        }

        return result;
    }

    // 获取礼盒id列表
    public static List<Long> getUnionGiftBoxIdList(long unionId) {
        List<Long> result = new ArrayList<>();
        List<Model> list = EntityManager.getEntityList(UnionGiftBoxList.class, unionId);
        if (list != null) {
            for (Model model : list) {
                UnionGiftBoxList unionGiftBoxList = (UnionGiftBoxList) model;
                result.add(unionGiftBoxList.getId());
            }
        }
        return result;
    }

    // 获取用户的联盟宝箱信息
    public static UserUnionBox getUserUnionBox(long userId) {
        return (UserUnionBox) EntityManager.getEntity(UserUnionBox.class, userId);
    }

    // 获取礼盒领取记录列表
    public static List<UserUnionGiftBoxRecord> getUserUnionGiftBoxRecordList(long userId) {
        List<UserUnionGiftBoxRecord> result = new ArrayList<>();
        List<Model> list = EntityManager.getEntityList(UserUnionGiftBoxRecord.class, userId);
        if (list != null) {
            for (Model model : list) {
                UserUnionGiftBoxRecord userUnionGiftBoxRecord = (UserUnionGiftBoxRecord) model;
                result.add(userUnionGiftBoxRecord);
           }
        }

        return result;
    }

    public static List<UnionUnlockDecoration> getUnionUnlockDecorationList(long unionId) {
        List<UnionUnlockDecoration> result = new ArrayList<>();
        List<Model> list = EntityManager.getEntityList(UnionUnlockDecoration.class, unionId);
        if (list != null) {
            for (Model model : list) {
                UnionUnlockDecoration decoration = (UnionUnlockDecoration) model;
                result.add(decoration);
            }
        }
        return result;
    }

    // 获取指定的地标建筑
    public static UnionUnlockDecoration getUnionUnlockDecorationById(long unionId,int decorationId) {
        List<Model> list = EntityManager.getEntityList(UnionUnlockDecoration.class, unionId);
        if (list != null) {
            for (Model model : list) {
                UnionUnlockDecoration decoration = (UnionUnlockDecoration) model;
                if (decoration.getId() == decorationId) {
                    return decoration;
                }
            }
        }
        return null;
    }


    public static UserUnionReciprocity getUserUnionReciprocity(long userId) {
        return (UserUnionReciprocity) EntityManager.getUniqueEntity(UserUnionReciprocity.class, userId);
    }

    public static UnionReciprocityList getUnionReciprocity(long unionId, long dbid) {
        return (UnionReciprocityList) EntityManager.getEntity(UnionReciprocityList.class, unionId, dbid);
    }

    // 获取联盟求助信息列表
    public static List<UnionReciprocityList> getUnionReciprocityList(long unionId) {
        List<UnionReciprocityList> result = new ArrayList<>();
        List<Model> list = EntityManager.getEntityList(UnionReciprocityList.class, unionId);
        if (list!= null) {
            for (Model model : list) {
                UnionReciprocityList unionReciprocityList = (UnionReciprocityList) model;
                if (TimeUtils.getTimeDiff(unionReciprocityList.getActivateTime()) > AllParam.UNION_RECIPROCITY_SUSTAIN_TIME || unionReciprocityList.isEventComplte()) {
                    unionReciprocityList.delete();// 求助对应的事件已经过期， 或是 超过7天的删除
                } else {
                    result.add(unionReciprocityList);
                }
            }
        }
        return result;
    }

    // 获取帮助记录列表
    public static List<UserUnionReciprocityRecord> getUserUnionReciprocityRecordList(long userId) {
        List<UserUnionReciprocityRecord> result = new ArrayList<>();
        List<Model> list = EntityManager.getEntityList(UserUnionReciprocityRecord.class, userId);
        if (list!= null) {
            for (Model model : list) {
                UserUnionReciprocityRecord userUnionReciprocityRecord = (UserUnionReciprocityRecord) model;
                if (TimeUtils.getTimeDiff(userUnionReciprocityRecord.getActivateTime()) > AllParam.UNION_RECIPROCITY_SUSTAIN_TIME) {
                    userUnionReciprocityRecord.delete();
                } else {
                    result.add(userUnionReciprocityRecord);
                }
            }
        }
        return result;
    }

    public static UnionDynamicRecord getUnionDynamicRecord(long unionId,long dbId) {
        return (UnionDynamicRecord) EntityManager.getEntity(UnionDynamicRecord.class, unionId,dbId);

    }
    public static List<UnionDynamicRecord> getUnionDynamicRecordList(long unionId) {
        List<UnionDynamicRecord> result = new ArrayList<>();
        List<Model> list = EntityManager.getEntityList(UnionDynamicRecord.class, unionId);
        if (list != null) {
            for (Model model : list) {
                UnionDynamicRecord unionDynamicRecord = (UnionDynamicRecord) model;
                result.add(unionDynamicRecord);
            }
        }
        return result;
    }

}
